<template>
	<view class="index-section">
		<tit tit="学才艺" url="/pages/tabBar/learning" />
		<view class="clickableInfo">
			<clickBtn :tabList="tabList" @cur="cur" ref="clickBtn" paddingStyle="26"/>
		</view>
		<view class="main">
			<view class="item" v-for="(item,index) in list" :key="index" @tap.stop="handleDetail(item.id)">
				<view class="top1" v-if="index ==0">
					<image :src="$imageOss(item.image)" mode="widthFix" style="" class="image"></image>
					<view class="top" style="margin-top: 10rpx;">
						{{item.titile}}
					</view>
					<view class="bot" style="height: 60rpx;">
						<text class="bot1">{{item.application_start_time}} | {{item.address}}</text>
						<view class="bot2"> 剩余名额：<text style="color: #FFB578;">{{item.left}}</text>人</view>
					</view>
				</view>
				<view class="other" v-else>
					<view class="left">
						<image :src="$imageOss(item.image)" mode="aspectFill" style="" class="image"></image>
					</view>
					<view class="right">
						<view class="top">
							{{item.titile}}
						</view>
						<view class="bot">
							<text class="bot1">{{item.application_start_time}} | {{item.address}}</text>
							<view class="bot2"> 剩余名额：<text style="color: #FFB578;">{{item.left}}</text>人</view>
						</view>
					</view>
				</view>
			</view>
			<view style="width: 100%; text-align: center; font-size: 28rpx; margin-top: 10rpx;">{{isText}}</view>
		</view>
	</view>
</template>

<script>
	import tit from '@/pages/index/cpns/title.vue';
	import clickBtn from '@/pages/index/cpns/clickBtn.vue';
	import {
		trainCategory,
		trainList
	} from "@/request/homeApi.js"
	export default {
		components: {
			tit,
			clickBtn,
		},
		data() {
			return {
				tabList: [],
				list: [],
				train_category_id: 1,
				isText: ''
			};
		},
		mounted() {
			this.getCategory()
		},
		methods: {
			cur(e) {
				this.train_category_id = e
				this.getList()
			},
			getCategory() {
				let that = this
				trainCategory().then(res => {
					if (res.code == 200) {
						that.tabList = res.data
						that.train_category_id = res.data.length > 0 ? res.data[0].id : ''
						that.$refs.clickBtn.changeIndex()
						that.getList()
					}
				})
			},
			getList() {
				let that = this

				trainList({
					train_category_id: that.train_category_id,
					page: 1,
					limit: 2,
					title: '',
					type: '',
					recommend: 2
				}).then(res => {
					if (res.code == 200) {
						that.list = res.data.list.map(item => {
							return {
								...item,
								left: item.limit - item.registered
							}
						})
						res.data.count == 0 ? that.isText = '暂无课程' : that.isText = ''
					}

				})
			},
			handleDetail(id) {
				uni.navigateTo({
					url: `/pages/home/learnDetails?id=${id}`,
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.index-section {
		padding: 20rpx;
		margin-top: 20rpx;
		background-color: #fff;

		.clickableInfo {
			margin: 20rpx 0;
		}

		.main {
			display: flex;
			flex-direction: column;

			.item {
				.top1 {
					display: flex;
					flex-direction: column;
					border-bottom: 2rpx solid #D9D9D9;

					.top {
						font-size: 30rpx;
					}

					.image {
						width: 100%;
						height: 290rpx !important;
						border-radius: 20rpx;
					}

					.bot {
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						margin-top: 16rpx;

						.bot1 {
							color: #909090;
							font-family: PingFang SC;
							font-size: 24rpx;
							font-style: normal;
							font-weight: 400;
							line-height: normal;
						}

						.bot2 {
							font-size: 20rpx;
							font-style: normal;
							font-weight: 400;
							line-height: normal;
							// margin-top: 16rpx;
							border-radius: 5px;
							background: rgba(0, 0, 0, 0.70);
							position: relative;
							top: -196rpx;
							left: 10rpx;
							padding: 10rpx 20rpx;
							width: 26%;
							color: #fff;
						}
					}
				}

				.other {
					display: flex;
					justify-content: space-between;
					margin-top: 20rpx;
					flex-direction: row-reverse;

					.left {
						width: 250rpx;
						height: 152rpx;
						margin-left: 16rpx;

						.image {
							border-radius: 8rpx;
							width: 100%;
							height: 100%;
						}
					}

					.right {
						flex: 1;
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						font-size: 30rpx;

						.bot {
							display: flex;
							flex-direction: column;
							justify-content: space-between;

							.bot1 {
								color: #909090;
								font-family: PingFang SC;
								font-size: 24rpx;
								font-style: normal;
								font-weight: 400;
								line-height: normal;
							}

							.bot2 {
								color: #313131;
								font-family: PingFang SC;
								font-size: 22rpx;
								font-style: normal;
								font-weight: 400;
								line-height: normal;
								margin-top: 10rpx;
							}
						}
					}
				}
			}
		}

	}
</style>
